package com.honeyms.project.blog.close.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honeyms.framework.aspectj.lang.annotation.Log;
import com.honeyms.framework.aspectj.lang.enums.BusinessType;
import com.honeyms.framework.web.controller.BaseController;
import com.honeyms.framework.web.domain.AjaxResult;
import com.honeyms.framework.web.page.BlogPageDomain;
import com.honeyms.framework.web.page.TableDataInfo;
import com.honeyms.project.blog.blogcomment.domain.BlogComment;
import com.honeyms.project.blog.blogcomment.service.IBlogCommentService;
import com.honeyms.project.blog.blogcontent.domain.BlogContent;
import com.honeyms.project.blog.blogcontent.service.IBlogContentService;
import com.honeyms.project.blog.blogcontentdetail.domain.BlogContentDetail;
import com.honeyms.project.blog.blogcontentdetail.service.IBlogContentDetailService;
import com.honeyms.project.blog.blogfavoritesusercontent.domain.BlogFavoritesUserContent;
import com.honeyms.project.blog.blogfavoritesusercontent.service.IBlogFavoritesUserContentService;
import com.honeyms.project.blog.blogzanusercontent.domain.BlogZanUserContent;
import com.honeyms.project.blog.blogzanusercontent.service.IBlogZanUserContentService;
import com.honeyms.project.blog.userfans.domain.UserFans;
import com.honeyms.project.blog.userfans.service.IUserFansService;
import com.honeyms.project.system.userdetail.domain.UserDetail;
import com.honeyms.project.system.userdetail.service.IUserDetailService;

/**
 * 需认证之后才可访问
 * @author honey
 * @version 创建时间：2020年11月18日 上午9:11:31
 */
@RequestMapping("/blog/close")
@Controller
public class BlogCloseController extends BaseController{
	
	@Autowired
    private IBlogContentService blogContentService;

	@Autowired
	private IUserDetailService userDetailService;
	
	@Autowired
    private IUserFansService userFansService;
	
	@Autowired
	private IBlogCommentService blogCommentService;
	
	@Autowired
	private IBlogContentDetailService blogContentDetailService;
	
    @Autowired
    private IBlogZanUserContentService blogZanUserContentService;
    
    @Autowired
    private IBlogFavoritesUserContentService blogFavoritesUserContentService;
	
  	@GetMapping("/followlist")
  	@ResponseBody
  	public TableDataInfo follow(BlogPageDomain blogPageDomain) {
  		UserFans userFans = new UserFans();
  		userFans.setFansId(getUserId());
  		String ids="";
  		List<UserFans> fans = userFansService.selectUserFansList(userFans);
  		for (UserFans fan : fans) {
			ids +=fan.getUserId()+",";
		}
    	startPage(blogPageDomain.getPageNum(),blogPageDomain.getPageSize());
  		List<BlogContent> list = blogContentService.selectBlogContentListByFollow(ids);
  		return getDataTable(list);
  	}
  	
	/**
	 * 用户点赞
	 * @param contentId
	 * @return
	 */
    @PostMapping("/zan")
    @ResponseBody
    public AjaxResult blogzanusercontent(@RequestParam("cid") Long contentId)
    {
    	BlogZanUserContent blogZanUserContent = new BlogZanUserContent();
    	blogZanUserContent.setUserId(getUserId());
    	blogZanUserContent.setContentId(contentId);
    	List<BlogZanUserContent> list = blogZanUserContentService.selectBlogZanUserContentList(blogZanUserContent);
    	BlogContentDetail blogContentDetail = blogContentDetailService.selectBlogContentDetailById(contentId);
    	if (list.isEmpty()) {
    		if (blogZanUserContentService.insertBlogZanUserContent(blogZanUserContent)>0) {
    			blogContentDetail.setZan(blogContentDetail.getZan()+1);
    			blogContentDetailService.updateBlogContentDetail(blogContentDetail);
				return AjaxResult.success("1", "点赞成功");
			}
		}else {
			if (blogZanUserContentService.deleteBlogZanUserContentById(getUserId(), contentId)>0) {
				blogContentDetail.setZan(blogContentDetail.getZan()-1);
    			blogContentDetailService.updateBlogContentDetail(blogContentDetail);
				return AjaxResult.success("2","取消点赞");
			}
		}
    	return toAjax(0);
    }
    
    /**
     * 用户收藏
     * @param contentId
     * @return
     */
    @PostMapping("/favorites")
    @ResponseBody
    public AjaxResult blogfavoritesusercontent(@RequestParam("cid") Long contentId)
    {
    	BlogFavoritesUserContent blogFavoritesUserContent = new BlogFavoritesUserContent();
    	blogFavoritesUserContent.setUserId(getUserId());
    	blogFavoritesUserContent.setContentId(contentId);
    	List<BlogFavoritesUserContent> list = blogFavoritesUserContentService.selectBlogFavoritesUserContentList(blogFavoritesUserContent);
    	BlogContentDetail blogContentDetail = blogContentDetailService.selectBlogContentDetailById(contentId);
    	if (list.isEmpty()) {
    		if (blogFavoritesUserContentService.insertBlogFavoritesUserContent(blogFavoritesUserContent)>0) {
    			blogContentDetail.setFavorites(blogContentDetail.getFavorites()+1);
    			blogContentDetailService.updateBlogContentDetail(blogContentDetail);
				return AjaxResult.success("1", "收藏成功");
			}
		}else {
			if (blogFavoritesUserContentService.deleteBlogFavoritesUserContentById(getUserId(), contentId)>0) {
				blogContentDetail.setFavorites(blogContentDetail.getFavorites()-1);
    			blogContentDetailService.updateBlogContentDetail(blogContentDetail);
				return AjaxResult.success("2","取消收藏");
			}
		}
    	return toAjax(0);
    }
    
    /**
     * 用户关注
     * @param contentId
     * @return
     */
    @PostMapping("/follow")
    @ResponseBody
    public AjaxResult follow(@RequestParam("uid") Long userId)
    {
    	if(getSysUser()==null) {
    		return AjaxResult.success("3","你还未登录!");
    	}
    	if (userId.equals(getUserId())) {
    		return AjaxResult.success("3","不能自己关注自己哦!");
    	}
    	UserFans userFans = new UserFans();
    	userFans.setUserId(userId);
    	userFans.setFansId(getUserId());
    	List<UserFans> list = userFansService.selectUserFansList(userFans);
    	UserDetail user = userDetailService.selectUserDetailById(userId);
    	UserDetail fans = userDetailService.selectUserDetailById(getUserId());
    	if (list.isEmpty()) {
    		if (userFansService.insertUserFans(userFans)>0) {
    			user.setFans(user.getFans()+1);
    			userDetailService.updateUserDetail(user);
    			fans.setFocus(fans.getFocus()+1);
    			userDetailService.updateUserDetail(fans);
    			return AjaxResult.success("1", "已关注");
    		}
    	}else {
    		if (userFansService.removeFans(userId, getUserId())>0) {
    			user.setFans(user.getFans()-1);
    			userDetailService.updateUserDetail(user);
    			fans.setFocus(fans.getFocus()-1);
    			userDetailService.updateUserDetail(fans);
    			return AjaxResult.success("2","已取消");
    		}
    	}
    	return toAjax(0);
    }
    
    /**
     * 发布评论
     * @param blogComment
     * @return
     */
    @Log(title = "评论详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BlogComment blogComment)
    {
    	if (getSysUser()==null) {
			return error("请登录之后再发表评论!");
		}
    	blogComment.setUserId(getUserId());
    	blogComment.setCreateDate(new Date());
		//status:1为评论;2:为回复
		if (blogComment.getType()==null) {
			blogComment.setType(1);
		}
        return toAjax(blogCommentService.insertBlogComment(blogComment));
    }
    
    /**
     * 删除评论
     * @param blogComment
     * @return
     */
    @Log(title = "删除评论", businessType = BusinessType.DELETE)
    @PostMapping("/blogcomment/remove")
    @ResponseBody
    public AjaxResult removeComment(Long commentId)
    {
    	BlogComment blogComment = blogCommentService.selectBlogCommentById(commentId);
    	if (!blogComment.getUserId().equals(getUserId())) {
    		return error("你只可以删除自己的评论!");
    	}
    	return toAjax(blogCommentService.deleteBlogCommentById(commentId));
    }
}
